<?php
class Authen
{
	var $error_msg;
	function isEmail($email)
	{
		$result	= (strlen($email)>6) && preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $email);
		if(empty($result)){
			$this->error_msg	=	'Email format error! ';
			return false;
		}
		return true;
	}
	function isUsername($username)
	{
		if(empty($username)){
			$this->error_msg	=	'username format error ';
			return false;
		}
		return true;
	}
	function isGroup($group)
	{
		if(!in_array($group, AC::$roles)){
			$this->error_msg	=	'error group ';
			return false;
		}
		return true;
	}
	function isStatus($status)
	{
		if(!in_array($status, array(0, 1))){
			$this->error_msg	=	'error status';
			return false;
		}
		return true;
	}
	function isScheduleTime($time)
	{
		$time	=	trim($time);
		if(empty($time))	{
			$this->error_msg	=	'Schedule time cant\' be empty ';
			return false;
		}
		$tmp	=	explode(" ", $time);
		
		$data	=	explode('-', $tmp[0]);
		
		if(count($data) == 3){
			if(checkdate ($data[1], $data[2], $data[0])){
				return true;
			}
		}
		$this->error_msg	=	'Schedule time wrong format!'.$time;
		return false;
	}
	function isScheduleID($id)
	{
		$schedule	=	new Schedule();
		$data		=	$schedule->getById($id);
		if(!empty($data)){
			return true;
		}
		return false;
	}
	function isReplaceKeyArr($keys)
	{
		$property		=	new Property();
		$replace_keys	=	$property->getReplaceKey();
		for($i = 0; $i < count ( $keys ); $i ++) {
			if(!in_array($keys[$i], $replace_keys)){
				$this->error_msg	=	'Wrong replace key!';
				return false;
			}
		}
		return true;
	}
	function isReplaceValueArr($values)
	{
		
		for($i = 0; $i < count ( $values ); $i ++) {
			$tmp	=	trim($values[$i]);
			if(empty($tmp)){
				$this->error_msg	=	'Replace value can not by empty!';
				return false;
			}
		}
		return true;
	}
	function isProgrammeTabArr($programme_id, $data)
	{
		
		$tabs	=	new Tabs();
		$tabs_id	=	$tabs->getProgrammeTabID($programme_id);
		
		foreach($data as $key=>$value)
		{
			if(!in_array($value, $tabs_id)){
				$this->error_msg	=	'Error Tab id :'.$value;
				return false;
			}
		}
		return true;
	}
	function isLanguage($lang_ids)
	{
		if(!empty($lang_ids)){
			foreach($lang_ids as $lang_id){
				if(!in_array($lang_id, array(AC::ENGLISH, AC::CHINESE))){
						$this->error_msg	=	'not a language ID';
						return false;
				}
			}
		}
		return true;
	}
	function checkEnglistCustomTab($programme_id, $lang_ids)
	{
		$tabs	=	new Tabs();
		$custom_tab_ids	=	$tabs->getCustomName($programme_id);
		if(!empty($lang_ids)){
			if(!empty($custom_tab_ids) && in_array(AC::ENGLISH, $lang_ids)){
				foreach($custom_tab_ids as $value){
					$tab_name_list[$value['tab_id']][$value['language_id']]	=	$value['name'];
					if(!empty($value['name']) ){
						$counter[$value['tab_id']][]	=	$value['language_id'];
					}
				}
				
				foreach($counter as $tab_id => $value){
					$result	=	array_diff(array(AC::ENGLISH, AC::CHINESE), $value);
					if(!empty($result)){
						$error_tab_name[]	=	$tab_name_list[$tab_id][AC::CHINESE];
					}
				}
			}
		}
		if(!empty($error_tab_name)){
			$this->error_msg	=	implode(', ', $error_tab_name).' englist tab name had empty value, please check!';
			return false;
		}
		return true;
	}
	function isProgrammeID($id)
	{
		$programmme	=	new Programme();
		$programme_list	=	$programmme->getWSProgrammeIDList();
		
		if(empty($id))	return false;
		if(in_array($id, $programme_list)){
			return true;
		}else{
			$this->error_msg	=	'ProgrammeID wrong format!';
			return false;
		}
	}
	function isScheduleType($type)
	{
		if(in_array($type, array(1, 2))){
			return true;
		}
		$this->error_msg	=	'not a schedule type';
		return false;
	}
	function isScheduleDateType($type)
	{
		if(in_array($type, array(0, 1))){
			return true;
		}
		$this->error_msg	=	'not a schedule date type';
		return false;
	}
	function isTabID($id)
	{
		if($id == 'new'){
			return true;
		}
		if(intval($id) == $id){
			return true;
		}
		$this->error_msg	=	'Custom Tab ID wrong format!';
		return false;
	}
	function isType($type)
	{
		$type	=	strtolower($type);
		if(in_array($type, array('file', 'url'))){
			return true;
		}
		$this->error_msg	=	'Type must be file or url!';
		return false;
	}
	function isTypeValue($value)
	{
		$value	=	trim($value);
		if(empty($value)){
			$this->error_msg	=	'Value can not empty!';
			return false;
		}
		return true;
	}
	function isColor($color)
	{
		$color	=	strtolower($color);
		
		if(strlen($color) != 7 || strlen($color) != 4){
			$this->error_msg	=	'A wrong format of color value!';
		 	return false;
		}
		if($color[0] !='#'){
			$this->error_msg	=	'A wrong format of color value!';
			return false;
		}
		return true;
	}
	function isTabOrder($order)
	{
		return true;
	}
	function isCnAddTabName($programme_id, $value)
	{
		$value	=	strtolower(trim($value));
		if(empty($value)){
			$this->error_msg	=	'CN tab name can not empty!';
			return false;
		}
		$tabs	=	new Tabs();
		$result	=	$tabs->getTabName($programme_id);
		if(!empty($result)){
			foreach($result as $k=>$v){
				$name	=	strtolower(trim($v['name']));
				if($name == $value && $v['language_id'] == AC::CHINESE){
					$this->error_msg	=	'Tab - already exist! ';
					return false;
				}
			}
		}
		return true;
	}
	function isEnAddTabName($programme_id, $value)
	{
		$value	=	strtolower(trim($value));
		if(empty($value)){
			return true;
		}
		$tabs	=	new Tabs();
		$result	=	$tabs->getTabName($programme_id);
		if(!empty($result)){
			foreach($result as $k=>$v){
				$name	=	strtolower(trim($v['name']));
				if($name == $value && $v['language_id'] == AC::ENGLISH){
					$this->error_msg	=	'Tab - already exist! ';
					return false;
				}
			}
		}
		return true;
	}
	function isCnUpdateTabName($programme_id, $tab_id, $value)
	{
		$value	=	strtolower(trim($value));
		if(empty($value)){
			$this->error_msg	=	'CN tab name can not empty!';
			return false;
		}
		$tabs	=	new Tabs();
		$result	=	$tabs->getTabName($programme_id);
		$i		=	0;
		if(!empty($result)){
			$tmp		=	array();
			foreach($result as $k=>$v  ){
				$name	=	strtolower(trim($v['name']));
				if($name == $value && $v['language_id'] == AC::CHINESE && $v['tab_id'] != $tab_id){
					$tmp[]	=	$name;
					
				}
			}
			
			if(count($tmp) >= 1){
				$this->error_msg	=	'Tab - already exist! ';
				return false;
			}
		}
		return true;
	}
	function isEnUpdateTabName($programme_id, $tab_id, $value)
	{
		$value	=	strtolower(trim($value));
		if(empty($value)){
			return true;
		}
		//var_dump($value);
		$tabs	=	new Tabs();
		$result	=	$tabs->getTabName($programme_id);
		$i		=	0;
		if(!empty($result)){
			$tmp		=	array();
			foreach($result as $k=>$v  ){
				$name	=	strtolower(trim($v['name']));
				if($name == $value && $v['language_id'] == AC::ENGLISH && $v['tab_id'] != $tab_id){
					$tmp[]	=	$name;
					
				}
			}
			
			if(count($tmp) >= 1){
				$this->error_msg	=	'Tab - already exist! ';
				return false;
			}
		}
		return true;
	}
	function getErrorMessage()
	{
		if(!empty($this->error_msg)){
			return $this->error_msg;
		}
		return false;
	}
}


?>